package com.yihaodian.server.servlet.rest;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yihaodian.cluster.ClusterService;
import com.yihaodian.cluster.ClusterState;

/**
 *  rest/cluster
 *  rest/cluster/nodes
 *  rest/cluster/node
 *  rest/cluster/routings
 *  rest/cluster/metas
 *  
 *  
 *  http://127.0.0.1:8080/search/rest/cluster
 * 
 */
public class ClusterServlet extends AbstractServlet {

	ClusterService clusterService = null;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = req.getRequestURI();

		ClusterState cs = clusterService.state();
		Object ret = cs;
		if (path != null) {
			if (path.contains("nodes"))
				ret = cs.nodes();
			else if(path.contains("node"))
				ret = clusterService.getLocalNode();
			else if (path.contains("routings"))
				ret = cs.routingTables();
			else if (path.contains("metas"))
				ret = cs.indexMetas();
		}

		super.respJson(req, resp, ret);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	public void init() throws ServletException {
		super.init();

		clusterService = injector.getInstance(ClusterService.class);
	}
}
